﻿using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetworkOPSUI
{
    public class SSHTool
    {
        private SshClient sshClient;
        private ShellStream shellStream;
        private RichTextBox outputBox;

        public SSHTool(string host, string username, string password, RichTextBox outputBox)
        {
            this.outputBox = outputBox;
            sshClient = new SshClient(host, username, password);
        }

        public void AddRichTextBox(RichTextBox richTextBox, string text)
        {
            richTextBox.AppendText(text);

            // 将光标设置到文本的末尾
            richTextBox.SelectionStart = richTextBox.Text.Length;

            // 滚动到光标位置
            richTextBox.ScrollToCaret();
        }

        public bool Connect()
        {
            if (!sshClient.IsConnected)
            {
                sshClient.Connect();
                shellStream = sshClient.CreateShellStream("custom_terminal", 80, 24, 800, 600, 1024);
                return true;
            }
            return false;



        }

        public bool Disconnect()
        {
            if (sshClient.IsConnected)
            {
                shellStream.Close();
                sshClient.Disconnect();
                return true;
            }
            else return false;

        }

        public void SendCommand(string command)
        {
            if (sshClient.IsConnected)
            {
                var cmd = sshClient.CreateCommand(command);
                cmd.CommandTimeout = TimeSpan.FromSeconds(10);
                var result = cmd.Execute();
                outputBox.Invoke(new Action(() =>
                {

                    AddRichTextBox(outputBox, result);
                }));
            }
            else
            {
                throw new InvalidOperationException("SSH client is not connected.");
            }
        }

        public void SendCommandWithShellStream(string command)
        {
            if (sshClient.IsConnected && shellStream != null)
            {
                shellStream.WriteLine(command);
                System.Threading.Thread.Sleep(1000);  // Give it some time. Adjust as needed.

                StringBuilder resultBuilder = new StringBuilder();
                while (shellStream.DataAvailable)
                {
                    resultBuilder.Append((char)shellStream.ReadByte());
                }

                string result = resultBuilder.ToString();

                outputBox.Invoke(new Action(() =>
                {
                    // outputBox.AppendText(result);
                    AddRichTextBox(outputBox, result);
                }));
            }
            else
            {
                throw new InvalidOperationException("SSH client or shell stream is not active.");
            }
        }


        ~SSHTool()
        {
            Disconnect();
        }
    }
}
